using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public List<GameObject> foodsPrefabs;
    private int index;
    public  TextMeshProUGUI scoreText;
    public GameObject gameOver;
    int score;
    public bool stop = false;
    public ParticleSystem[] particleSystems;
    public Button restartBtn;

    public GameObject difficultyParent;
    private float waitSeconds;
    // Start is called before the first frame update
    void Start()
    {
       // InvokeRepeating("Spawn", 3f, 3f);
        restartBtn.onClick.AddListener(Restart);
    }
    // Update is called once per frame
    void Update()
    {
        if(stop)
        {
            gameOver.gameObject.SetActive(stop);
        }
    }


    IEnumerator Spawn()
    {
        while(!stop) 
        {
            yield return new WaitForSeconds(waitSeconds);
            index = Random.Range(0, foodsPrefabs.Count);
            Instantiate(foodsPrefabs[index], RandomPos(), transform.rotation);
        }
    }

    public ParticleSystem SpawnParticle(int index,Vector3 pos,Quaternion rot)
    {
        return Instantiate(particleSystems[index],pos,rot);
    }

    Vector3 RandomPos()
    {
        return new Vector3(Random.Range(-4f,4f),0,0);
    }

    public void UpdateScore(int num)
    {
        score += num;
        scoreText.text = "Score: " + score;

    }

    public void Restart()
    {
        SceneManager.LoadScene("Prototype 5");

    }

    public void GameStart(float interval)
    {
        waitSeconds = interval;
        StartCoroutine(Spawn());
        difficultyParent.gameObject.SetActive(false);
    }
}